package com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.RiskAssessmentContracts;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.bean.RiskAssessmentBean;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.model.impl.RiskAssessmentDetailModel;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.presenter.IRiskAssessmentDetailPresenter;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.presenter.impl.RiskAssessmentDetailPresenter;
import com.yuexunit.operationman.nbsy.module.OperationRiskAssessment.ui.view.IRiskAssessmentDetailView;
import com.yuexunit.operationman.nbsy.util.ProUtil;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/8/9 0009.
 */

public class Act_RiskAssessmentMain extends BaseKJActivity implements IRiskAssessmentDetailView {

    @Bind(R.id.titlebar_tv_title)
    TextView titlebarTvTitle;
    @Bind(R.id.riskassessment_pic)
    ImageView riskassessmentPic;
    @Bind(R.id.riskassessment_name)
    TextView riskassessmentName;
    @Bind(R.id.riskassessment_age)
    TextView riskassessmentAge;
    @Bind(R.id.riskassessment_sex)
    TextView riskassessmentSex;
    @Bind(R.id.riskassessment_position)
    TextView riskassessmentPosition;
    @Bind(R.id.patient)
    RelativeLayout patient;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.riskassessment_bed)
    TextView riskassessmentBed;
    @Bind(R.id.riskassessment_way)
    TextView riskassessmentWay;
    @Bind(R.id.riskassessment_operation)
    TextView riskassessmentOperation;

    IRiskAssessmentDetailPresenter presenter;

    LoadingDialog loadingDialog;


    @Override
    public void setRootView() {
        setContentView(R.layout.act_riskassessmentmain);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new RiskAssessmentDetailPresenter(this, new RiskAssessmentDetailModel());
    }

    @Override
    public void onResume() {
        super.onResume();
        Map<String, String> params = new HashMap<>();
        params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
        presenter.getRiskAssessmentDate(params);
    }

    private void setTitle() {
        titlebarTvTitle.setText("手术风险评估单");
    }

    @OnClick({R.id.titlebar_img_back, R.id.riskassessment_ll_01, R.id.riskassessment_ll_02, R.id.riskassessment_ll_03, R.id.riskassessment_ll_04, R.id.riskassessment_ll_05, R.id.riskassessment_ll_06, R.id.riskassessment_ll_07, R.id.riskassessment_ll_08})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.riskassessment_ll_01:
                startActivity(new Intent(aty, Act_RiskAssessmentClean.class));
                break;
            case R.id.riskassessment_ll_02:
                startActivity(new Intent(aty, Act_RiskAssessmentScale.class));
                break;
            case R.id.riskassessment_ll_03:
                startActivity(new Intent(aty, Act_RiskAssessmentTime.class));
                break;
            case R.id.riskassessment_ll_04:
                startActivity(new Intent(aty, Act_RiskAssessmentStatus.class));
                break;
            case R.id.riskassessment_ll_05:
                startActivity(new Intent(aty, Act_RiskAssessmentEvaluate.class));
                break;
            case R.id.riskassessment_ll_06:
                startActivity(new Intent(aty, Act_RiskAssessmentVisit.class));
                break;
            case R.id.riskassessment_ll_07:
                startActivity(new Intent(aty, Act_RiskAssessmentSign.class));
                break;
            case R.id.riskassessment_ll_08:
                startActivity(new Intent(aty, Act_RiskAssessmentPreview.class));
                break;
        }
    }

    @Override
    public void getRiskAssessmentDate(RiskAssessmentBean riskAssessmentBean) {
        if (AppConfig.isEye && null == riskAssessmentBean.getObject().getRisk().getRiskId()) {
            String safe = ProUtil.getJson("Risk.json",aty);
            RiskAssessmentBean riskAssessmentBean1= JSON.parseObject(safe,RiskAssessmentBean.class);
            if (riskAssessmentBean.getCode() == 200) {
                riskAssessmentBean1.getObject().setRisk(riskAssessmentBean.getObject().getRisk());
                AppConfig.RISKASSESSMENTBEAN = riskAssessmentBean1;

                riskassessmentName.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientName());
                riskassessmentAge.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientAge());
                if (AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientSex().equals("1")) {
                    riskassessmentSex.setText("男");
                } else if (AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientSex().equals("2")) {
                    riskassessmentSex.setText("女");
                } else {
                    riskassessmentSex.setText("不详");
                }
                riskassessmentPosition.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getDepartment());
                riskassessmentBed.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getBedPosition());
                riskassessmentWay.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getOperandi());
                riskassessmentOperation.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getInHospitalNo());
                if (AppConfig.RISKASSESSMENTBEAN.getObject().getRiskItemList() == null) {
                    AppConfig.RISKASSESSMENTBEAN.getObject().setRiskItemList(RiskAssessmentContracts.riskItemListEntities_new(this));
                }
            }
        } else {
            if (riskAssessmentBean.getCode() == 200) {
                AppConfig.RISKASSESSMENTBEAN = riskAssessmentBean;
                riskassessmentName.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientName());
                riskassessmentAge.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientAge());
                if (AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientSex().equals("1")) {
                    riskassessmentSex.setText("男");
                } else if (AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getPatientSex().equals("2")) {
                    riskassessmentSex.setText("女");
                } else {
                    riskassessmentSex.setText("不详");
                }
                riskassessmentPosition.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getDepartment());
                riskassessmentBed.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getBedPosition());
                riskassessmentWay.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getOperandi());
                riskassessmentOperation.setText(AppConfig.RISKASSESSMENTBEAN.getObject().getRisk().getInHospitalNo());
                if (AppConfig.RISKASSESSMENTBEAN.getObject().getRiskItemList() == null) {
                    AppConfig.RISKASSESSMENTBEAN.getObject().setRiskItemList(RiskAssessmentContracts.riskItemListEntities_new(this));
                }
            }
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
